package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.wemedia.service.WmNewsMaterialService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Autowired
    WmNewsService wmNewsService;
    @Autowired
    WmNewsMaterialService wmNewsMaterialService;


    @PostMapping("list")
    ResponseResult list(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }


    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {
        return wmNewsService.submitNews(dto);
    }

    @GetMapping("/one/{id}")
    public ResponseResult one(@PathVariable("id") Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmNews news = wmNewsService.getById(id);
        if (news == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章不存在");
        }
        return ResponseResult.okResult(news);
    }

    @GetMapping("/del_news/{id}")
    public ResponseResult del_news(@PathVariable("id") Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmNews wmNews = wmNewsService.getById(id);
        if (wmNews == null) {
            return ResponseResult.errorResult(1002, "文章不存在");
        }
        if (WmNews.Status.PUBLISHED.getCode() == wmNews.getStatus()) {
            return ResponseResult.errorResult(501, "文章已发布，不可删除");
        }
        boolean b = wmNewsService.removeById(id);
        if (b) {
            wmNewsMaterialService.remove(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getNewsId, id));
        }
        return ResponseResult.success();
    }

    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmNews wmNews = wmNewsService.getById(dto.getId());
        if (wmNews == null) {
            return ResponseResult.errorResult(1002, "文章不存在");
        }
        if (WmNews.Status.PUBLISHED.getCode() != wmNews.getStatus()) {
            return ResponseResult.errorResult(501, "文章未发布，不可上下架");
        }
        WmNews entity = new WmNews();
        entity.setId(wmNews.getId());
        entity.setEnable(wmNews.getEnable() == (short) 1 ? (short) 0 : (short) 1);
        wmNewsService.updateById(entity);
        return ResponseResult.success();
    }
}
